<?php

/**
 * Created by PhpStorm.
 * User: Evan
 * Date: 16/3/14
 * Time: 下午1:06
 */
class Friend_link extends MY_Agent_Controller
{
    public function index()
    {
        $limit = 20;
        $currentPage = (int)$this->input->get_post('per_page');
        $offset = ($currentPage < 1) ? 0 : ($currentPage - 1) * $limit;


        $name   = trim($this->input->get_post('name'));
        $time   = $this->input->get_post('create_time');
        $status = $this->input->get_post('status');



        $where = array();
        if (!empty($name))
            $where['name like \'%'.$name.'%\''] = null;

        if ($time)
        {
            $eTime = explode('-', $time);
            $where['create_time >='] = date('Y-m-d H:i:s', strtotime($eTime[0]));
            $where['create_time <='] = date('Y-m-d ', strtotime($eTime[1])).'23:59:59';
        }
        ($status || $status === '0') && $where['status'] = $status;

        $this->load->model('Model_link', 'link');

        $totalNum = $this->link->count($where);
        $info = $this->link->lists($offset, $limit, $where);


        $this->load->library('pagination');
        $config = $this->page_config('Friend_link/index', $totalNum, $limit, http_build_query($where));
        $this->pagination->initialize($config);
        $pageHtml = $this->pagination->create_links();


        $data = $this->link->lists(0, 100000, array(), 'name');


        $data = array(
            'title'    => '资讯列表',
            'info'     => $info,
            'data'     => $data,
            'pageHtml' => $pageHtml,
            'time'     => $time,
            'status'   => $status,
            'name'     => $name,
            'totalNum' => $totalNum,
        );

        $this->load->view('agent/link/index', $data);
    }


    public function create()
    {
        $lid = (int)$this->uri->segment(3);


        $item = array();
        if ($lid)
        {
            $this->load->model('Model_link', 'link');
            $item = $this->link->getById($lid);
        }


        $data = array(
            'title'     => '编辑友情链接',
            'data'      => $item,
        );

        $this->load->view('agent/link/create', $data);
    }


    public function save()
    {
        $lid  = (int)$this->input->get_post('lid');
        $icon = (int)$this->input->get_post('icon');


        $data = array(
            'name'   => trim($this->input->get_post('name')),
            'link'   => trim($this->input->get_post('link')),
            'sort'   => (int)$this->input->get_post('sort'),
            'status' => (int)$this->input->get_post('status'),
            'update_time' => nowTime(),
        );

        $icon && $data['icon'] = $icon;
        !$lid && $data['create_time'] = nowTime();


        if (empty ($data['name']))
            show_error('标题为空!');


        $this->load->model('Model_link', 'link');
        $rs = $this->link->save($data, $lid ? $lid : null);

        if (!$rs)
            show_error('保存失败!');


        $this->load->helper('url');
        redirect(url('admin').'Friend_link/index');
    }


    public function delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $lid = (int)$this->input->get_post('lid');

            if (!$lid)
            {
                $response = error(100000);
                break;
            }


            $this->load->model('Model_link', 'link');
            $item = $this->link->getById($lid);

            if (empty ($item))
            {
                $response = error(100002);
                break;
            }


            $res = $this->link->save(array('status' => FLS_DELETE), $lid);
            if (!$res)
            {
                $response = error(100003);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}